package com.example.wechat.csdnandroid.service;
 
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;


import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.example.wechat.csdnandroid.R;



public class ForegroundService extends Service {
 
  /**
  * id不可设置为0,否则不能设置为前台service
  */
  private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;
  private static  int NOTIFICATION_DOWNLOAD_PROGRESS_ID_ADD = 1;

  private boolean isRemove=false;//是否需要移除
 
  /**
  * Notification
  */
  public void createNotification(){
    String CHANNEL_ONE_ID = "CHANNEL_ONE_ID";
    String CHANNEL_ONE_NAME= "CHANNEL_ONE_ID";
    NotificationChannel notificationChannel= null;
//进行8.0的判断
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      notificationChannel= new NotificationChannel(CHANNEL_ONE_ID,
              CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
      notificationChannel.enableLights(true);
      notificationChannel.setLightColor(Color.RED);
      notificationChannel.setShowBadge(true);
      notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
      NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      manager.createNotificationChannel(notificationChannel);
    }
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mp624183768"));
    PendingIntent pendingIntent= PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification= null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      notification = new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
              .setTicker("Nature")
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentTitle("这是一个测试标题")
              .setContentIntent(pendingIntent)
              .setContentText("这是一个测试内容")
              .build();
    }
    notification.flags|= Notification.FLAG_NO_CLEAR;
    startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID_ADD++, notification);
  }
 
 
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    int i=intent.getExtras().getInt("cmd");
    if(i==0){
      if(!isRemove) {
        createNotification();
      }
      isRemove=true;
    }else {
      //移除前台服务
      if (isRemove) {
        stopForeground(true);
      }
      isRemove=false;
    }
 
    return super.onStartCommand(intent, flags, startId);
  }
 
  @Override
  public void onDestroy() {
    //移除前台服务
    if (isRemove) {
      stopForeground(true);
    }
    isRemove=false;
    super.onDestroy();
  }
 
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
}